package com.health.ts.utils;

import java.util.Map;
import java.util.Scanner;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.TemplateConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

/**
 * <p>
 * 代码生成器
 * </p>
 */
public class CodeGeneratorUtil {
	
	/**
     * <p>
     * 读取控制台内容
     * </p>
     */
	@SuppressWarnings("resource")
    public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 数据源配置
        String url = "jdbc:mysql://localhost:3306/ts?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true";
        String username = "root";
        String password = "123456";
        DataSourceConfig ds = new DataSourceConfig.Builder(url, username, password).build();
        
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator(ds);

        // 全局配置
//      String outputDir = System.getProperty("user.dir") + "/src/main/java";
        String outputDir = "D://codeGenerator";
        GlobalConfig globalConfig = new GlobalConfig.Builder()
        		.outputDir(outputDir)
        		.author("css")
        		.enableSwagger()
        		.fileOverride().build();

        // 包配置
        PackageConfig packageConfig = new PackageConfig.Builder("com.health.ts", scanner("模块名")).build();
        
        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig
                .Builder()
                .mapperXml("/templates/mapper.xml")
                .build();
        
        // 策略配置
        StrategyConfig strategyConfig = new StrategyConfig
                .Builder()
                .addInclude(scanner("表名，多个英文逗号分割").split(","))
                .entityBuilder()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .enableActiveRecord()
                .idType(IdType.ASSIGN_ID)
                .enableTableFieldAnnotation()
                .enableLombok()
                .mapperBuilder()
                .enableBaseResultMap()
                .enableBaseColumnList()
                .controllerBuilder()
                .enableRestStyle()
                .build();

		// 自定义配置
        InjectionConfig injectionConfig = new InjectionConfig
        		.Builder()
        		.beforeOutputFile((table, map)->{
        	        // 自定义 Mapper XML 生成目录
                    ConfigBuilder config = (ConfigBuilder)map.get("config");
                    Map<OutputFile, String> pathInfoMap = config.getPathInfo();
                    pathInfoMap.put(OutputFile.mapperXml, pathInfoMap.get(OutputFile.mapperXml).replaceAll("/java.*","/resources/mapper"));
                    map.put("config", config);
        		}).build();
        
        mpg.global(globalConfig);
        mpg.packageInfo(packageConfig);
        mpg.template(templateConfig);
        mpg.strategy(strategyConfig);
        mpg.injection(injectionConfig);
        mpg.execute(new FreemarkerTemplateEngine());
    }
}